package com.gm.wholesale.module.custom.ui.customManger;

import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.interfaces.ICallBackListener;
import com.gm.commonlib.interfaces.IResultListener;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.custom.api.ICustomApi;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.view.CustormFunctionView;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.good.api.IGoodApi;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.view.GoodFunctionView;

import java.util.Date;
import java.util.HashMap;

import io.reactivex.Observable;

public class CustomListFragment extends BaseGridFragment<CustomBean, BasePageData<CustomBean>> {
    @Override
    protected void onCreateContentHeader(ViewGroup parent) {
      View view=  LayoutInflater.from(getContent()).inflate(R.layout.fragment_custorm_manger, parent);
        view.findViewById(R.id.createBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EvenBusUtil.showCreateCustormUI(null);
            }
        });
    }

    @Override
    public void onRefreshView() {
        hidCurrentfunctionView();
        super.onRefreshView();
    }

    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        setBodybg(getResources().getColor(R.color.page_bg));
        mGridView.setNumColumns(3);
        mGridView.setHorizontalSpacing(15);
        mGridView.setVerticalSpacing(15);
        setBodyPadding(10);

    }

    private CustormFunctionView mCurrentfunctionView;



    @Override
    protected BaseAdapter createTempBaseAdapter() {
        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_item_custorm) {
            @Override
            public void convert(ViewHolder viewHolder, final CustomBean data) {

                boolean isTodayTime = DateTimeUtils.isToday(data.getLastOrderTime());
                if (isTodayTime) {
                    viewHolder.getView(R.id.tagView).setVisibility(View.VISIBLE);
                } else {
                    viewHolder.getView(R.id.tagView).setVisibility(View.GONE);
                }
                viewHolder.setText(R.id.custormNameTv, data.getName());
                String contactPhone = data.getContactPerson() + " " + data.getContactPhone();
                viewHolder.setText(R.id.contactPhoneTv, "联系方式:" + getTextData(contactPhone));
                viewHolder.setText(R.id.contactAddressTv, "送货地址:" + getTextData(data.getAddress()));
                CustormFunctionView functionView = viewHolder.getView(R.id.custormFunctionView);
                viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        hidCurrentfunctionView();
                        mCurrentfunctionView = functionView;
                        functionView.showView(data, new IResultListener<CustomBean>() {
                            @Override
                            public void onResult(CustomBean result) {
                                if (result!=null){
                                    hidCurrentfunctionView();
                                    ToastUtil.showToast("删除成功");
                                    refreshData();
                                }
                                else {
                                    hidCurrentfunctionView();
                                }

                            }
                        });
                        return false;
                    }
                });

                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mCurrentfunctionView != null) {
                            mCurrentfunctionView.hidView();
                            mCurrentfunctionView = null;
                            return;
                        }
                        EvenBusUtil.showCreateCustormUI(data);
                    }
                });

            }
        };
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
       if (event.getCode()== EventBusType.UPDATE_CUSTORM_MANGER_LIST.getType()){
           refreshData();
       }
    }

    @Override
    public void onTouchClick() {
        hidCurrentfunctionView();
    }

    private void hidCurrentfunctionView() {
        if (mCurrentfunctionView != null) {
            mCurrentfunctionView.hidView();
            mCurrentfunctionView = null;
        }
    }


    private String getTextData(String text) {
        if (TextUtils.isEmpty(text) || TextUtils.isEmpty(text.trim())) {
            return "-";
        }
        return text;
    }

    @Override
    protected HttpServicesHolder<?, BasePageData<CustomBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<ICustomApi, BasePageData<CustomBean>>() {
            @Override
            public Observable<BasePageData<CustomBean>> getHttpObservable(ICustomApi api) {
                HashMap<String, Object> queryData = new HashMap<>();
                queryData.put("page", index);
                queryData.put("pageSize", 20);
                return api.getCustomers(queryData);
            }
        };
    }
}
